package abel.wlmr.model.unit;

import abel.wlmr.model.WlmrBaseUnit;
import abel.wlmr.model.WlmrException;
import io.netty.buffer.ByteBuf;

/**
 * 曲线类数据时标 Td_c 见本附录 B.5.11.1.3.3 7
 * 正向流量示值 1 见附录 C.29 单位代号 5
 * 
 * @author Abel
 *
 */
public class AfnThirdF1_2DownUnit extends WlmrBaseUnit {

    private static final short FIXED_LENGTH = 1 + 1;

    private short pm;

    private short pn;

    public AfnThirdF1_2DownUnit() {
        super();
    }

    public AfnThirdF1_2DownUnit(short guessedLen) {
        super(guessedLen);
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitBody#appendBuf(io.netty.buffer.ByteBuf)
     */
    @Override
    public ByteBuf appendBuf(ByteBuf buf) {
        if (pm != (pm & 0xFF) || pn != (pn & 0xFF)) {
            throw new IllegalArgumentException("pm/pn should be [0,255]");
        }

        buf.writeByte(pm);
        buf.writeByte(pn);

        return buf;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitBody#parseBuf(io.netty.buffer.ByteBuf)
     */
    @Override
    public int parseBuf(ByteBuf buf) throws WlmrException {
        if (checkBuffer(buf) < 0) {
            return -1;
        }

        pm = (short) (buf.readByte() & 0xFF);
        pn = (short) (buf.readByte() & 0xFF);

        return FIXED_LENGTH;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.WlmrDataPart#getMinLength()
     */
    @Override
    public int getMinLength() {
        return FIXED_LENGTH;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.AppUnitBody#getFinalLen()
     */
    @Override
    public short getFinalLen() {
        return FIXED_LENGTH;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{pm:%s, pn:%s}", pm, pn);
    }

    /**
     * @return the pm
     */
    public short getPm() {
        return pm;
    }

    /**
     * @param pm
     *            the pm to set
     */
    public void setPm(short pm) {
        this.pm = pm;
    }

    /**
     * @return the pn
     */
    public short getPn() {
        return pn;
    }

    /**
     * @param pn
     *            the pn to set
     */
    public void setPn(short pn) {
        this.pn = pn;
    }
}
